//
//  YWResetPasswordViewController.m
//  maintain
//
//  Created by Kyo on 15/11/2.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWResetPasswordViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWResetPasswordCell.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "MBProgressHUD.h"
#import "YWConst.h"
#import "YWLoginTool.h"
#import "YWLoginUser.h"
#import "NSString+md5.h"

@interface YWResetPasswordViewController ()<UITableViewDataSource,UITableViewDelegate>{
}
@property(nonatomic, strong) UITableView *myTableView;
@property(nonatomic, strong) NSArray *titleArray;
@property(nonatomic, strong) NSArray *placeHolderArray;
@property(nonatomic, copy) NSString *theNewPassword;
@property (nonatomic, weak) MBProgressHUD* HUD;
@end

@implementation YWResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"setupPassWord", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self initTableView];
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        NSString *passWord = NSLocalizedString(@"passWord", nil);
        NSString *makeSurePassWord = NSLocalizedString(@"makeSurePassWord", nil);
        NSArray *titleArr = @[passWord,makeSurePassWord];
        _titleArray = titleArr;
    }
    return _titleArray;
}

- (NSArray *)placeHolderArray{
    if (!_placeHolderArray) {
        NSString *setupLoginPassWord = NSLocalizedString(@"setupLoginPassWord", nil);
        NSString *writeAgain = NSLocalizedString(@"writeAgain", nil);
        NSArray *placeH = @[setupLoginPassWord,writeAgain];
        _placeHolderArray = placeH;
    }
    return _placeHolderArray;
}

- (MBProgressHUD*)HUD
{
    if (!_HUD) {
        MBProgressHUD* hud = [[MBProgressHUD alloc] initWithView:self.view];
        hud.labelFont = kHUDLabelFont;
        hud.labelText = NSLocalizedString(kTextBeingProcessed, nil);
        hud.dimBackground = YES;
        // hud.animationType = MBProgressHUDAnimationZoom;
        _HUD = hud;
        _HUD.tag = kMBProgressHUDTag;
        [self.view addSubview:hud];
    }
    return _HUD;
}

- (void)initTableView
{
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor customBackgroudColor];
    tableView.separatorColor = [UIColor customLineColor];
    tableView.sectionFooterHeight = 1.0;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    _myTableView = tableView;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"finish", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickFinishBarButton:)];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    YWResetPasswordCell *cell = [YWResetPasswordCell cellWithTableView:tableView];
    cell.title = self.titleArray[indexPath.row];
    cell.placeHolder = self.placeHolderArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDataDelegate mothods
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

#pragma mark - Checking FinishBarButtonItem
- (void)clickFinishBarButton:(UIBarButtonItem*)item
{
    [self.view endEditing:YES];
    YWResetPasswordCell *cell = nil;
    NSString *psw1 = nil;
    NSString *psw2 = nil;
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:0];
    cell = [_myTableView cellForRowAtIndexPath:indexPath1];
    psw1 = cell.passwordTF.text;
    
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:1 inSection:0];
    cell = [_myTableView cellForRowAtIndexPath:indexPath2];
    psw2 = cell.passwordTF.text;
    
    if (psw1.length==0 || psw2.length==0) {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextPasswordNotNull, nil)];
        return;
    }
    if (![psw1 isEqualToString:psw2]) {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextPasswordNotSame, nil)];
        return;
    }else{
        _theNewPassword = psw2;
    }
    //提交修改后的密码
    if ([[WMHttpTool shareHttpTool] existenceNetwork]) {
        [self.HUD show:YES];
        [self requestModifyPassword];
    }
    else {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
    }
}

- (void)requestModifyPassword{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {return;}
    
    YWLoginUser* loginUser = [YWLoginTool loginUser];
    NSDictionary* params = @{ @"oldPwd" : [loginUser.userPwd md5]?:@"",
                              @"newPwd" : [_theNewPassword md5]?:@""};
    
    [[WMHttpTool shareHttpTool] putWithUrl:[YWCommonTool modifyPasswordUrl] params:params success:^(NSHTTPURLResponse* response, id json) {
        [self.HUD hide:YES];
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextModifyPasswordSuccess, nil)];
        YWLoginUser* user = [YWLoginTool loginUser];
        user.userPwd = _theNewPassword;
        [YWLoginTool saveLoginOfUser:user];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        [self.HUD hide:YES];
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextModifyPasswordFail, nil)];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
